package ru.sis.shop.servlets;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.LoadingCache;

/**
 * Servlet implementation class GoodsController.
 * 
 * @author sis
 * 
 */
@WebServlet("/goods")
public final class GoodsController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2822684170301697307L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
			IOException {

		final SessionFactory factory = (SessionFactory) getServletContext().getAttribute("sessionFactory");
		final Session session = factory.openSession();

		try {
			request.setAttribute("goodsItems", session.createQuery("from Item").list());
			final RequestDispatcher dispatcher = request.getRequestDispatcher("goods.jsp");
			dispatcher.forward(request, response);
		} finally {
			session.close();
		}

	}
}
